<template>
  <div>
    <Spin :spinning="loading" tip="Loading...">
      <div style="padding-top: 1rem;"></div>
      <!-- table -->
       <FormPanel ref="formPanel" :list="formList1" :cols="1" :formType="'add'" labelWidth="110" :isSubmitBtn="false" @formChange="formChangeHandle" />
    <div style="padding-top: 1rem;"></div>
      <div
        :style="{
          position: 'absolute',
          left: 0,
          right: 0,
          zIndex: 9,
          borderTop: '1px solid #e9e9e9',
          padding: '10px 20px',
          background: '#fff',
          textAlign: 'right'
        }"
      >
        <multiuse-button type="primary" :click="saveDialog.bind()">保 存</multiuse-button>
        <multiuse-button @click="closeDialog">关 闭</multiuse-button>
      </div>
    <BaseDialog :visible.sync="visibleVehicle" :title="'经销商选择'" destroyOnClose width="850px" :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
      <vehicle @close="closeVehicle" @save="saveVehicle"></vehicle>
    </BaseDialog>
    </Spin> 
  </div>
</template>
<script>
import DealerList from '@/pages/claimDemage/tools/DealerList.vue';
 import vehicle from '@/pages/claimDemage/tools/DealerList.vue';
//获取当前经销商
import {getOwnerCode} from '@/assets/js/auth';
import { selectById, editSave } from '@/api/claim/factory/PartAndDealer';
//import { getListByPage, newSave, piliangqiyong, piliangtingyong, excelExport, editSave } from '@/api/claim/factory/PartRecoveryCoefficient';

export default {
  props: [ 'rowData'],
  components: {
    DealerList,
    vehicle  
  },
  data() {
    return {
      loading: false,
      dataList1: [],
      visible: false,
      visibleDrawer: false,
      collapse: false,
      infoId: null,
      flterValue: {}, //参数
      fetchParams: {      
        }, //ipa 接口
      selectedRows: [], //选中经销商
      formList1: this.createFormList1(),
      saveData: null,
      visibleVehicle: false,
    };
  },
  created() {
    //this.chuqi();
  },
   mounted() {
    this.init();
  },
  methods: {
    dealerTurnOn() {
      this.visibleVehicle = true;
    },
    closeVehicle() {
      this.visibleVehicle = false;
    },
    saveVehicle(obj) {
      this.visibleVehicle = false;
      this.formList1.find(x => x.fieldName == 'ownerCode').initialValue = obj[0].dealerCode;
      this.formList1.find(x => x.fieldName == 'dealerName').initialValue = obj[0].dealerShortname;
    },
    formChangeHandle(val) {
      console.log(val);
      this.saveData = val;
    },
    //保存
    async saveDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
      if (this.saveData !== null) {
        this.loading = true;
        let res = await editSave(this.saveData);
        console.log(res);
        if (res.resultCode === 200) {
          this.$emit('save');
          return this.$notify.success({title: '提示信息', message: '保存成功'});
        } else {
          this.loading = false;
          // this.$notify.error({title: '提示信息', message: res.errMsg || '保存失败!'});
          // this.closeDialog();
        }
      } else {
        // return this.$notify.warning({title: '提示信息', message: '有未校验通过的信息'});
      }
    },
    //取消操作
    closeDialog() {
      this.$emit('close', {visible: false, data: this.selectedRows, type: 'close'});
    },
    add() {
    },
    // async chuqi() {
    //    const res = await getOwnerCode();
    //   if (res.resultCode == 200) {
    //     this.formList1.find((x) => x.fieldName == 'dealerCode').initialValue = '';
    //     this.formList1.find((x) => x.fieldName == 'dealerName').itemList = res.data.map((y) => ({ text: y.name, value: y.code }));
    //   }
    // },
    //回显
    async init() { 
      this.loading = true;
      let newVar = await selectById({id: this.rowData.pdId});
      this.formList1.forEach(x => {
        x.initialValue = newVar.data[x.fieldName];
      });
      this.newVar = newVar.data;
      this.loading = false;
    },
    chooseOrderNo() {
      this.visible = true;
    },
    save(obj) {
      console.log(obj);
      this.formList1.find(x => x.fieldName == 'ownerCode').initialValue = obj[0].dealerCode;
      this.formList1.find(x => x.fieldName == 'para').initialValue = obj[0].para;
      this.visible = false;
    },
    close() {
      this.visible = false;
    },
    createFormList1() {
      return [
        {
          type: 'INPUT',
          label: '经销商代码',
          fieldName: 'ownerCode',
          placeholder: '请输入经销商代码...',
          initialValue: '',
          offsetRightCols: 1,
          unitRender: p => {
            return (
              <div>
                <multiuse-button icon="el-icon-search" onClick={() => this.dealerTurnOn()} />
              </div>
            );
          }
        },
        {
          type: 'INPUT',
          label: '经销商名称',
          readonly: true,// 只读
          disabled: true,// 置会
          fieldName: 'dealerName',
          placeholder: '请输入经销商名称...',
          rules: [{ required: true, message: '请输入经销商名称...', trigger: 'blur' }],
          offsetRightCols: 1
        },
        {
          type: 'INPUT',
          label: '故障件运费系数',
          readonly: false,// 只读
          disabled: false,// 置会
          fieldName: 'para',
          placeholder: '故障件运费系数...',
          rules: [{ required: true, message: '故障件运费系数...', trigger: 'blur' }],
          offsetRightCols: 1
        }

      ];
    }
  }
};
</script>

<style lang="less" scoped></style>
